<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/21 0021
 * Time: 下午 3:31
 */
namespace app\api\controller;

use app\admin\model\OrderAfterSale;
use app\admin\model\Orderitem;
use app\common\validate\Refund;
use think\Db;
use think\Exception;
use think\Request;

class Aftersale extends Base{
    //申请售后
    //订单售后
    public function refund(Request $request){
        if($request->isPost()){
            //1.订单确认收货后不能再申请售后
            //2.商户拒绝售后之后不能再重新申请
            //2.到店订单核销之后不能再申请售后
            (new Refund())->goCheck();
            $param = $request->param();
            $order = new \app\api\service\Aftersale();
            return $order->refund($param);
        }else{
            return show(config('status.ERROR_STATUS'),'非法请求','非法请求');
        }

    }
    //计算售后金额
    public function getRefund_fee($order_id,$product_id){
        $Aftersale = new \app\api\service\Aftersale();
        $res = $Aftersale->getRefund_fee($order_id,$product_id);

        return show(config('status.SUCCESS_STATUS'),'ok',$res);

    }

    /**
     * 用户退货 发物流
     * @param Request $request
     * @return \think\response\Json
     */
    public function afterReturn_goods(Request $request){
        if($request->isPost()){
            $param = $request->param();
            if(!empty($param['id']) && !empty($param['express_name']) && !empty($param['express_no'])){
                try{
                    $data = OrderAfterSale::where('id',$param['id'])->find();
                    if($data){
                        if($data->status == 4){
                            $data->status = 5;
                            $data->express_name = $param['express_name'];
                            $data->express_no = $param['express_no'];
                            $data->save();
                            return show(config('status.SUCCESS_STATUS'),'操作成功','操作成功');
                        }else{
                            return show(config('status.ERROR_STATUS'),'售后订单状态异常',[]);
                        }
                    }else{
                        return show(config('status.ERROR_STATUS'),'售后订单不存在',[]);
                    }
                }catch (Exception $ex){
                    return show(config('status.ERROR_STATUS'),$ex->getMessage(),[]);
                }

            }else{
                return show(config('status.ERROR_STATUS'),'参数错误',[]);
            }
        }else{
            return show(config('status.ERROR_STATUS'),'非法请求',[]);
        }
    }

    /**
     * 用户取消订单售后
     * @param Request $request
     * @return \think\response\Json
     */
    public function cancelRefund(Request $request)
    {
        $param = $request->param();
        if (empty($param['id'])) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '售后订单ID不能为空');
        }
        Db::startTrans();
        try {
            $OrderAfterSale = OrderAfterSale::where('id',$param['id'])->find();
            if($OrderAfterSale){
                if($OrderAfterSale->status == 1){
                    $OrderAfterSale->status = 8;
                    $OrderAfterSale->save();
                    Orderitem::where('id',$OrderAfterSale['order_item_id'])->update(['after_sale_status'=>5]);
                    Db::commit();
                    return show(config('status.SUCCESS_STATUS'), '取消成功', '取消成功');
                }else{
                    return show(config('status.ERROR_STATUS'), '售后订单状态异常', []);
                }
            }else{
                return show(config('status.ERROR_STATUS'), '售后订单不存在', []);
            }
        } catch (Exception $e) {
            Db::rollback();
            return show(config('status.ERROR_STATUS'), $e->getMessage(), '取消售后失败');
        }
    }

}